package com.tywj.lib.social.pay

import android.app.Activity
import android.text.TextUtils
import com.tywj.lib.social.entity.AlipayPayOrderEntity
import com.tywj.lib.social.entity.AlipayPayResult
import com.tywj.lib.social.interfaces.Callback
import com.tywj.lib.social.interfaces.IPay
import com.alipay.sdk.app.PayTask
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread

/**
 * <pre>
 *     author : June Yang
 *     time   : 2020/5/16
 *     desc   : 支付宝支付
 *     version: 1.0.0
 * </pre>
 */
class AlipayPay : IPay<AlipayPayOrderEntity> {

    override fun pay(entity: AlipayPayOrderEntity, activity: Activity, callback: Callback) {
        // 在没有安装支付宝的手机上，默认会调用H5页面进行支付，所以不需要判断用户是否安装支付宝
        doAsync {
            val alipay = PayTask(activity)
            val result = alipay.payV2(entity.str, true)
            val payResult = AlipayPayResult(result)
            uiThread {
                // 判断resultStatus 为9000则代表支付成功
                if (TextUtils.equals(payResult.resultStatus, "9000")) {
                    callback.onSuccess()
                } else {
                    callback.onError()
                }
            }
        }
    }
}